//----------------------------------Include-------------------------------------
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "mcu.h"
#include "board.h"
#include "glbvar.h"
//------------------------------------------------------------------------------
//global var defined in this file
#define  POWER_GLOBALS
#include "power.h"
//------------------------------------------------------------------------------

//----------------------------------Globals-------------------------------------
float        gf_ufreq;
float        gf_rmsIA;
float        gf_rmsIB;
float        gf_rmsU;
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
#define RUN_START        0
#define RUN_NORMAL       1
#define RUN_END          2
//------------------------------------------------------------------------------

//------------------------------------Type--------------------------------------
typedef struct{
  uint16_t   rms_IA;
  uint16_t   rms_IB;
  uint16_t   rms_U;
  uint16_t   power_PA;
  uint16_t   power_PB;
  uint16_t   power_S;
  uint16_t   energy_A;
  uint16_t   energy_B;
}t_cff;
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
static uint32_t    nu32_rs = RUN_START;
static uint64_t    nu64_time_tick = 0;
//------------------------------------------------------------------------------
t_cff              n_cff;

static float       nf_ufreq;
static float       nf_rmsIA;
static float       nf_rmsIB;
static float       nf_rmsU;
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
static void _check_cff(void)
{
  //read coefficient
  n_cff.rms_IA   = brd_hlw8112_rd_reg(RmsIAC, 2);
  n_cff.rms_IB   = brd_hlw8112_rd_reg(RmsIBC, 2);
  n_cff.rms_U    = brd_hlw8112_rd_reg(RmsUC,  2);
  
  n_cff.power_PA = brd_hlw8112_rd_reg(PowerPAC, 2);
  n_cff.power_PB = brd_hlw8112_rd_reg(PowerPBC, 2);
  n_cff.power_S  = brd_hlw8112_rd_reg(PowerSC,  2);
  
  n_cff.energy_A = brd_hlw8112_rd_reg(EnergyAC, 2);
  n_cff.energy_B = brd_hlw8112_rd_reg(EnergyBC, 2);
  
  uint16_t  chksum, temp;
  chksum = brd_hlw8112_rd_reg(CHECKSUM, 2);
  
  temp = ~(0xFFFF + n_cff.rms_IA   + n_cff.rms_IB   + n_cff.rms_U   + 
                    n_cff.power_PA + n_cff.power_PB + n_cff.power_S + 
                    n_cff.energy_A + n_cff.energy_B );
  if(chksum != temp) while(1);
}
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void power_cyc(void)
{
  switch(nu32_rs){
  case RUN_NORMAL:
    if(igu64_tick_ms > nu64_time_tick + 2000){
      nf_ufreq = brd_hlw8112_rd_reg(Ufreq, 2);
      nf_rmsIA = brd_hlw8112_rd_reg(RmsIA, 3);
      nf_rmsIB = brd_hlw8112_rd_reg(RmsIB, 3);
      nf_rmsU  = brd_hlw8112_rd_reg(RmsU,  3);
      
      gf_ufreq = 3579000.0/(8*nf_ufreq);
      gf_rmsIA = nf_rmsIA *n_cff.rms_IA *4/20000 /0x800000;
      gf_rmsIB = nf_rmsIB *n_cff.rms_IB *4/20000 /0x800000;
      gf_rmsU  = nf_rmsU  *n_cff.rms_U  *0.234   /0x400000;
    }
    break;
  case RUN_START:
    _check_cff();
    igu64_tick_ms = nu64_time_tick;
    nu32_rs = RUN_NORMAL;
    break;
  default: break;
  }
}
//------------------------------------EOF---------------------------------------



